package main

import "fmt"

//#link: https://leetcode-cn.com/problems/roman-to-integer/
func main() {
	s := "IV"
	fmt.Println(romanToInt(s))
}

var (
	//初始化字符map对应的数字
	letterMap = map[string]int{
		"I":  1,
		"IV": 4,
		"V":  5,
		"IX": 9,
		"X":  10,
		"XL": 40,
		"L":  50,
		"XC": 90,
		"C":  100,
		"CD": 400,
		"D":  500,
		"CM": 900,
		"M":  1000,
	}
)

//罗马数字转整数
func romanToInt(s string) int {
	result := 0
	sByte := []byte(s)
	for i := 0; i < len(sByte); i++ {
		letter := append([]byte(nil), sByte[i])
		if (i < len(sByte)-1) && (sByte[i] == 'I' || sByte[i] == 'X' || sByte[i] == 'C') {
			tempLetter := append(letter, sByte[i+1])
			if value, ok := letterMap[string(tempLetter)]; ok {
				result += value
				i++
				continue
			}
		}
		result += letterMap[string(letter)]
	}
	return result
}
